package com.lanou3g.code0515.view;

import com.lanou3g.code0515.controller.Controller;
import com.lanou3g.code0515.pojo.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dllo on 18/5/15.
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String username= request.getParameter("username");
        String password =request.getParameter("password");

      Controller controller =new Controller();
       User user = controller.login(username,password);
        //// TODO: 18/5/15  根据user是否为空 来判断 登录是否成功
        String msg="";
        if (user==null){
            msg="用户名/密码错误";
        }else {
            msg="登录成功";
        }
        //向request域中放值
        //值以key-value的形式存放
        //key只能是String,value可以是任意对象
        request.setAttribute("msg",msg);

        request.getRequestDispatcher("msg").forward(request,response);
       // response.getWriter().println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          this.doPost(request,response);
    }
}
